package ttOrder.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;
import ttOrder.dao.inter.OrderMapper;
import ttOrder.model.po.DinnerTable;
import ttOrder.model.po.Order;
import ttOrder.model.po.OrderDetail;
import ttOrder.model.vo.OrderDetailVO;
import ttOrder.model.vo.OrderVO;

@Service
@Transactional
public class OrderServiceImpl extends abstractService<Order>{

	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderDetailServiceImpl orderDetailService;
	@Autowired
	private DinnerTableServiceImpl dinnerTableServiceImpl;
	
	
	/* operate with vo class */

	/** 
	 * 更新总价
	 * 
	 * */
	public int updateTotalMoney(int orderId,int addMoney){
		Order o = getByKey(new Order(orderId));
		if (o != null) {
			o.setTotalPrice(o.getTotalPrice()+addMoney);
			update(o);
		}
		return 0;
	}
	
	/** 
	 * 结账操作
	 * 
	 * */
	public OrderVO orderCheckout(int tableId){
		if (tableId > 0) {
			try {
				OrderVO order = getVOByTableIdAndStatus(new OrderVO(tableId, 0));
				if (order != null ) {
					order.setOrderStatus(1);
					int f = update(order);
					
					if (f > 0) {
						dinnerTableServiceImpl.changeTableStatus(tableId, 0);
						return order;
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public List<OrderVO> getVOListByStatus(int status) {
		List<OrderVO> volist = new ArrayList<OrderVO>();
		List<Order> polist = getVOByStatus(status);
		if (polist.size() > 0) {
			OrderVO vo = null;
			for (Order po : polist) {
				vo = new OrderVO();
				BeanUtils.copyProperties(po, vo);
				List<OrderDetailVO> orderDetailList = orderDetailService.getDetailVOByOrderId(new OrderDetail(po.getId()));
				vo.setOrderDetailList(orderDetailList);
				vo.setDinnerTable(dinnerTableServiceImpl.getVOByKey(new DinnerTable(po.getTableId())));
				volist.add(vo);
			}
		}
		return volist;
	}

	public List<OrderVO> getAllVO() {
		List<OrderVO> volist = new ArrayList<OrderVO>();
		List<Order> polist = orderMapper.selectAll();
		if (polist.size() > 0) {
			OrderVO vo = null;
			for (Order po : polist) {
				vo = new OrderVO();
				BeanUtils.copyProperties(po, vo);
				List<OrderDetailVO> orderDetailList = orderDetailService.getDetailVOByOrderId(new OrderDetail(po.getId()));
				vo.setOrderDetailList(orderDetailList);
				vo.setDinnerTable(dinnerTableServiceImpl.getVOByKey(new DinnerTable(po.getTableId())));
				volist.add(vo);
			}
		}
		return volist;
	}
	
	public List<OrderVO> getAllVO(boolean nopeDetail) {
		List<OrderVO> volist = new ArrayList<OrderVO>();
		List<Order> polist = orderMapper.selectAll();
		if (polist.size() > 0) {
			OrderVO vo = null;
			for (Order po : polist) {
				vo = new OrderVO();
				BeanUtils.copyProperties(po, vo);
				vo.setDinnerTable(dinnerTableServiceImpl.getVOByKey(new DinnerTable(po.getTableId())));
				volist.add(vo);
			}
		}
		return volist;
	}
	
	public OrderVO getByKey(OrderVO keyvo) {
		//获取订单类的原有信息，将id信息全数转化为相应的实体类
		OrderVO order = new OrderVO(); 
		if (keyvo != null) {
			Order key = new Order();
			BeanUtils.copyProperties(keyvo, key);
			Order po = new Order();
			po = orderMapper.selectByPrimaryKey(key);
			BeanUtils.copyProperties(po, order);
			List<OrderDetailVO> orderDetailList = orderDetailService.getDetailVOByOrderId(new OrderDetail(po.getId()));
			order.setOrderDetailList(orderDetailList);
			order.setDinnerTable(dinnerTableServiceImpl.getVOByKey(new DinnerTable(po.getTableId())));
		}
		return order;
	}
	
	public int insert(OrderVO recordvo) {
		int f = 0;
		Order record = new Order(); 
		BeanUtils.copyProperties(recordvo, record);
		f = orderMapper.insertOrder(record);
		if (record.getId() > 0) {
			OrderDetail detailPO = null;
			for (OrderDetailVO detailVO : recordvo.getOrderDetailList()) {
				detailPO = new OrderDetail();
				BeanUtils.copyProperties(detailVO, detailPO);
				detailPO.setOrderId(record.getId());
				orderDetailService.insert(detailPO);
			}
			dinnerTableServiceImpl.changeTableStatus(record.getTableId(), 1);
			f = record.getId();
		}
		return f;
	}
	
	public int update(OrderVO recordvo) {
		Order record = new Order(); 
		BeanUtils.copyProperties(recordvo, record);
		return orderMapper.updateByPrimaryKeySelective(record);
	}
	
	public int deleteByKey(OrderVO keyvo) {
		Order key = new Order();
		BeanUtils.copyProperties(keyvo, key);
		//删除订单详情
		return orderMapper.deleteByPrimaryKey(key);
	}
	
	/* operate with po class */

	@Override
	public List<Order> getAll() {
		return orderMapper.selectAll();
	}
 
	@Override
	public List<Order> getByEntity(Order record) {
		return orderMapper.select(record);
	}

	@Override
	public Order getByKey(Order key) {
		return orderMapper.selectByPrimaryKey(key);
	}

	@Override
	public int insert(Order record) {
		return orderMapper.insert(record);
	}

	@Override
	public int update(Order record) {
		return orderMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int deleteByKey(Order key) {
		return orderMapper.deleteByPrimaryKey(key);
	}

	@Override
	public int deleteByEntity(Order record) {
		return orderMapper.delete(record);
	}

	@Override
	public int getCount(Order record) {
		return orderMapper.selectCount(record);
	}
	
	/**
	 * 根据当前实体类的tableId字段和orderStatus获取OrderVO
	 * 
	 * @author emoker 
	 * 
	 * @param 带tableId和OrderStatus的OrderVO
	 * 
	 * @return OrderVO
	 * 
	 * @see selectByExample(T t)
	 * 
	 * */
	public OrderVO getVOByTableIdAndStatus(OrderVO record) {
		Example e = new Example(Order.class); 
		Example.Criteria criteria = e.createCriteria();
		if (record.getTableId() > 0) {
			criteria.andEqualTo("tableId", record.getTableId());
			criteria.andEqualTo("orderStatus", record.getOrderStatus());
		}
		List<Order> list = orderMapper.selectByExample(e);
		if (list.size() >= 1) {
			return getByKey(new OrderVO(list.get(list.size()-1).getId()));
		}
		return null;
	}
	
	/**
	 * 根据当前传入的orderStatus获取OrderVO的集合
	 * 
	 * @author emoker 
	 * 
	 * @param int status
	 * 
	 * @return List<Order>
	 * 
	 * @see selectByExample(T t)
	 * 
	 * */
	public List<Order> getVOByStatus(int status) {
		Example e = new Example(Order.class); 
		Example.Criteria criteria = e.createCriteria();
		if (status == 0 || status == 1) {
			criteria.andEqualTo("orderStatus", status);
		}
		return orderMapper.selectByExample(e);
	}

}
